﻿using System.Collections.Generic;
using System.IO;
using System.Web.Hosting;

namespace visinia.SmartEngine.Installer
{
    public class InstallerLocalContentController
    {
        public static List<ContentFileInfo> GetLocalModules()
        {
            List<ContentFileInfo> ContentFileList = new List<ContentFileInfo>();
            string installFolder = HostingEnvironment.MapPath(WebRoots.GetInstallRoot);
            string[] ContentFiles = Directory.GetFiles(installFolder, "*.content");
            foreach (string contentFile in ContentFiles)
            {
                ContentFileList.AddRange(ContentFileParser.ParseContentFile(contentFile));
            }
            return ContentFileList.FindAll(p => p.Type == ContentFileType.Module);
        }

        public static List<ContentFileInfo> GetLocalPlugins()
        {
            List<ContentFileInfo> ContentFileList = new List<ContentFileInfo>();
            string installFolder = HostingEnvironment.MapPath(WebRoots.GetInstallRoot);
            string[] ContentFiles = Directory.GetFiles(installFolder, "*.content");
            foreach (string contentFile in ContentFiles)
            {
                ContentFileList.AddRange(ContentFileParser.ParseContentFile(contentFile));
            }
            return ContentFileList.FindAll(p => p.Type == ContentFileType.Plugin);
        }

        public static List<ContentFileInfo> GetLocalThemes()
        {
            List<ContentFileInfo> ContentFileList = new List<ContentFileInfo>();
            string installFolder = HostingEnvironment.MapPath(WebRoots.GetInstallRoot);
            string[] ContentFiles = Directory.GetFiles(installFolder, "*.content");
            foreach (string contentFile in ContentFiles)
            {
                ContentFileList.AddRange(ContentFileParser.ParseContentFile(contentFile));
            }
            return ContentFileList.FindAll(p => p.Type == ContentFileType.Theme);
        }

        public static List<string> GetLocalZipFiles()
        {            
            string installFolder = HostingEnvironment.MapPath(WebRoots.GetInstallRoot);
            string [] zipFiles =  Directory.GetFiles(installFolder, "*.zip");            
            List<string> zipNames = new List<string>();
            foreach (string zipFile in zipFiles)
            {
                zipNames.Add(Path.GetFileName(zipFile));
            }
            return zipNames;
        } 
        
        public static List<ContentFileInfo> GetLocalZipFilesContents()
        {
            List<ContentFileInfo> ContentFileList = new List<ContentFileInfo>();
            string installFolder = HostingEnvironment.MapPath(WebRoots.GetInstallRoot);
            string[] ContentFiles = Directory.GetFiles(installFolder, "*.content");
            foreach (string contentFile in ContentFiles)
            {
                ContentFileList.AddRange(ContentFileParser.ParseContentFile(contentFile));
            }
            return ContentFileList;
        }
    }
}
